import axios from 'axios'
import { AlertModule } from 'vux'
import Vue from 'vue'
import router from '../router'
import { baseUrl } from './env'

axios.defaults.timeout = 100000

// request拦截器
// var loadinginstace;
axios.interceptors.request.use(config => {
    Vue.$vux.loading.show()
    config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    config.transformRequest = [function(data, headers) {
        let dataStr = ''
        Object.keys(data).forEach(key => {
            dataStr += key + '=' + data[key] + '&'
        })

        if (dataStr !== '') {
            dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'))
        }
        return dataStr
    }]
    return config
}, error => {
    AlertModule.show({
        title: '提示',
        content: '加载超时'
    })
    return Promise.reject(error)
})

// response拦截器
axios.interceptors.response.use(data => {
    Vue.$vux.loading.hide()
    return data
}, error => {
    AlertModule.show({
        title: '提示',
        content: '加载失败'
    })
    return Promise.reject(error)
})

export default async(url, data, method = 'post', _base) => {
    method = method.toUpperCase();
    if (_base) {
        url = _base + url;
    } else {
        url = baseUrl + url
    }
    // 如果是get请求
    if (method == 'GET') {
        let dataStr = ''; //数据拼接字符串
        Object.keys(data).forEach(key => {
            dataStr += key + '=' + data[key] + '&';
        })

        if (dataStr !== '') {
            dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
            url = url + '?' + dataStr;
        }
    }
    let promise = new Promise(function(resolve, reject) {
        axios({
            method: method,
            url: url,
            data: data
        }).then(function(res) {
            if (res.status !== 200) {
                // store._actions.setSignOut[0]()
                // store._actions.removeRotateInfoArr[0]()
                AlertModule.show({
                    title: '提示',
                    content: res.data.msg,
                    onHide() {
                        router.push('/')
                    }
                })
                reject(res.data.msg)
            }
            if(res.data.status){
                if (res.data.status >= 200 && res.data.status < 300) {
                    resolve(res.data)
                } else {
                    if (res.data.status === 401 || res.data.status === 408) {
                        // store._actions.setSignOut[0]()
                        // store._actions.removeRotateInfoArr[0]()
                        localStorage.clear();
                        AlertModule.show({
                            title: '提示',
                            content: res.data.msg,
                            onHide() {
                                router.push('/login')
                            }
                        })
                    } else {
                        AlertModule.show({
                            title: '提示',
                            content: res.data.msg
                        })
                    }
                    reject(res.data)
                }
            }else{
                resolve(res.data)
            }

        }).catch(function(err) {
            AlertModule.show({
                title: '提示',
                content: '主人，您的网络有点差，请检查下您的网络',
                onHide() {
                	Vue.$vux.loading.hide()
                    router.push('/')
                }
            })
            reject(err)
        })
    })
    return promise
}
